package com.nio.c1_NettyTest;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

// 第一例demo  只是 了解 byteBuffer.allocate 的分配空间！  channel.read 写入 缓冲区数据！   buffer.get() 得到数据
@Slf4j
public class TestByteBufferAllocate {
    public static void main(String[] args) {
        System.out.println(ByteBuffer.allocate(10).getClass());
        System.out.println(ByteBuffer.allocateDirect(10).getClass());
/*
*      allocate     class java.nio.HeapByteBuffer   -java堆内存，读写效率低，受到GC影响
       allocateDirect     class java.nio.DirectByteBuffer  -直接内存，读写效率高（少一次拷贝），不会受GC影响，
       * 分配的效率低
*/
        Path path = Paths.get("G:\\MyProgran\\play\\Netty-demo\\src\\main\\resources\\data.txt");
        System.out.println(Files.exists(path));
        try (FileChannel channel = new FileInputStream("G:\\MyProgran\\play\\Netty-demo\\src\\main\\resources\\data.txt").getChannel()) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(10);
            //通过channel 写入数据
            int read = channel.read(byteBuffer);
            // 移动指针 转变为 读取模式
            byteBuffer.flip();

            byte b = byteBuffer.get();
            log.info("输出：{}", (char) b);


        } catch (IOException e) {
        }
    }
}
